package net.entelijan.cobean.examples.showcase.multisel;

import static org.junit.Assert.assertArrayEquals;

import java.util.List;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.IMultiSelectionListCobean;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.Locale;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.multisel.DefaultMultiselInitializer;
import net.entelijan.cobean.util.CobeanUtil;
import net.entelijan.cobean.util.PropertyComparator;

import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JListFixture;
import org.junit.Test;

@SuppressWarnings("unused")
public class SortMultiselFestTestCase {

	@Test
	public void test() {
		SortMultiselPanel panel = new SortMultiselPanel();
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture showcaseFix = new FrameFixture(frame);
		try {
			showcaseFix.show();

			DefaultMultiselInitializer<Locale> ini = new DefaultMultiselInitializer<Locale>();
			ini.setCoAddButton(panel.getMultiselPanel().getAddButton());
			ini.setCoRemoveButton(panel.getMultiselPanel().getRemoveButton());
			ini.setCoLeftList(panel.getMultiselPanel().getLeftList());
			ini.setCoRightList(panel.getMultiselPanel().getRightList());
			List<ColumnDesc> colDescs = ColumnDesc
					.createPropertyOnlyColumnDescs("ISO3Country",
							"ISO3Language", "displayCountry", "displayLanguage");
			ini.setColumnDescs(colDescs);
			ini.setSorted(true);
			ini.setComparator(new PropertyComparator<Locale>("ISO3Country",
					true));
			final IMultiSelectionListCobean<Locale> cobean = ini.initialize();
			final List<Locale> data = ExamplesUtil.createLocales();
			cobean.setValue(data);
			cobean.setSelectedValue(CobeanUtil.createListWithOneEntry(data
					.get(3)));

			JListFixture leftList = showcaseFix
					.list("DefaultMultiselPanel.leftList");
			JListFixture rightList = showcaseFix
					.list("DefaultMultiselPanel.rightList");
			JButtonFixture addButton = showcaseFix
					.button("DefaultMultiselPanel.addButton");
			// TODO Make some tests with the remove button
			// JButtonFixture removeButton = showcaseFix.button("DefaultMultiselPanel.removeButton");
			// Assert initial data
			{
				leftList.requireNoSelection();
				rightList.requireNoSelection();
				assertArrayEquals(getInitialLeftList(), leftList.contents());
				assertArrayEquals(getInitialRightList(), rightList.contents());
			}
			// Select one
			{
				leftList.selectItem(5);
				addButton.click();
				pauseShort();
				assertArrayEquals(getLeftList01(), leftList.contents());
				assertArrayEquals(getRightList01(), rightList.contents());
			}
			// Select first
			{
				leftList.selectItem(0);
				addButton.click();
				pauseShort();
				leftList.requireNoSelection();
				rightList
						.requireSelection("ara ara United Arab Emirates Arabic");
				assertArrayEquals(getLeftList02(), leftList.contents());
				assertArrayEquals(getRightList02(), rightList.contents());
			}
		} finally {
			pauseShort();
			showcaseFix.cleanUp();
		}
	}

	private String[] getInitialRightList() {
		return new String[] { "spa spa Panama Spanish" };
	}

	private String[] getInitialLeftList() {
		return new String[] { "ara ara United Arab Emirates Arabic",
				"ara ara Iraq Arabic", "ara ara Yemen Arabic",
				"ara ara Qatar Arabic", "ara ara Saudi Arabia Arabic",
				"ara ara Lebanon Arabic", "ara ara Kuwait Arabic",
				"ara ara Sudan Arabic", "ara ara Syria Arabic",
				"ara ara Bahrain Arabic", "ara ara Tunisia Arabic",
				"ara ara Jordan Arabic", "ara ara Egypt Arabic",
				"ara ara Morocco Arabic", "ara ara Algeria Arabic",
				"ara ara Libya Arabic", "ara ara Oman Arabic",
				"bel bel Belarus Belarusian", "bul bul Bulgaria Bulgarian",
				"cat cat Spain Catalan", "ces ces Czech Republic Czech",
				"dan dan Denmark Danish", "deu deu Switzerland German",
				"deu deu Austria German", "deu deu Germany German",
				"deu deu Luxembourg German", "ell ell Cyprus Greek",
				"ell ell Greece Greek", "eng eng United States English",
				"eng eng Malta English", "eng eng United Kingdom English",
				"eng eng New Zealand English", "eng eng Philippines English",
				"eng eng South Africa English", "eng eng Ireland English",
				"eng eng India English", "eng eng Australia English",
				"eng eng Canada English", "eng eng Singapore English",
				"est est Estonia Estonian", "fin fin Finland Finnish",
				"fra fra Belgium French", "fra fra Canada French",
				"fra fra Switzerland French", "fra fra Luxembourg French",
				"fra fra France French", "gle gle Ireland Irish",
				"heb heb Israel Hebrew", "hin hin India Hindi",
				"hrv hrv Croatia Croatian", "hun hun Hungary Hungarian",
				"ind ind Indonesia Indonesian", "isl isl Iceland Icelandic",
				"ita ita Italy Italian", "ita ita Switzerland Italian",
				"jpn jpn Japan Japanese", "jpn jpn Japan Japanese",
				"kor kor South Korea Korean", "lav lav Latvia Latvian",
				"lit lit Lithuania Lithuanian", "mkd mkd Macedonia Macedonian",
				"mlt mlt Malta Maltese", "msa msa Malaysia Malay",
				"nld nld Netherlands Dutch", "nld nld Belgium Dutch",
				"nor nor Norway Norwegian", "nor nor Norway Norwegian",
				"pol pol Poland Polish", "por por Portugal Portuguese",
				"por por Brazil Portuguese", "ron ron Romania Romanian",
				"rus rus Russia Russian", "slk slk Slovakia Slovak",
				"slv slv Slovenia Slovenian", "spa spa Peru Spanish",
				"spa spa Guatemala Spanish", "spa spa Nicaragua Spanish",
				"spa spa Spain Spanish", "spa spa United States Spanish",
				"spa spa Mexico Spanish", "spa spa Uruguay Spanish",
				"spa spa Dominican Republic Spanish",
				"spa spa Venezuela Spanish", "spa spa Ecuador Spanish",
				"spa spa Colombia Spanish", "spa spa Costa Rica Spanish",
				"spa spa Chile Spanish", "spa spa Bolivia Spanish",
				"spa spa El Salvador Spanish", "spa spa Paraguay Spanish",
				"spa spa Puerto Rico Spanish", "spa spa Argentina Spanish",
				"spa spa Honduras Spanish", "sqi sqi Albania Albanian",
				"srp srp Bosnia and Herzegovina Serbian",
				"srp srp Serbia and Montenegro Serbian",
				"srp srp Serbia Serbian", "srp srp Montenegro Serbian",
				"swe swe Sweden Swedish", "tha tha Thailand Thai",
				"tha tha Thailand Thai", "tur tur Turkey Turkish",
				"ukr ukr Ukraine Ukrainian", "vie vie Vietnam Vietnamese",
				"zho zho Taiwan Chinese", "zho zho Singapore Chinese",
				"zho zho China Chinese", "zho zho Hong Kong Chinese" };
	}

	private String[] getRightList01() {
		return new String[] { "ara ara Lebanon Arabic",
				"spa spa Panama Spanish" };
	}

	private String[] getLeftList01() {
		return new String[] { "ara ara United Arab Emirates Arabic",
				"ara ara Iraq Arabic", "ara ara Yemen Arabic",
				"ara ara Qatar Arabic", "ara ara Saudi Arabia Arabic",
				"ara ara Kuwait Arabic", "ara ara Sudan Arabic",
				"ara ara Syria Arabic", "ara ara Bahrain Arabic",
				"ara ara Tunisia Arabic", "ara ara Jordan Arabic",
				"ara ara Egypt Arabic", "ara ara Morocco Arabic",
				"ara ara Algeria Arabic", "ara ara Libya Arabic",
				"ara ara Oman Arabic", "bel bel Belarus Belarusian",
				"bul bul Bulgaria Bulgarian", "cat cat Spain Catalan",
				"ces ces Czech Republic Czech", "dan dan Denmark Danish",
				"deu deu Switzerland German", "deu deu Austria German",
				"deu deu Germany German", "deu deu Luxembourg German",
				"ell ell Cyprus Greek", "ell ell Greece Greek",
				"eng eng United States English", "eng eng Malta English",
				"eng eng United Kingdom English",
				"eng eng New Zealand English", "eng eng Philippines English",
				"eng eng South Africa English", "eng eng Ireland English",
				"eng eng India English", "eng eng Australia English",
				"eng eng Canada English", "eng eng Singapore English",
				"est est Estonia Estonian", "fin fin Finland Finnish",
				"fra fra Belgium French", "fra fra Canada French",
				"fra fra Switzerland French", "fra fra Luxembourg French",
				"fra fra France French", "gle gle Ireland Irish",
				"heb heb Israel Hebrew", "hin hin India Hindi",
				"hrv hrv Croatia Croatian", "hun hun Hungary Hungarian",
				"ind ind Indonesia Indonesian", "isl isl Iceland Icelandic",
				"ita ita Italy Italian", "ita ita Switzerland Italian",
				"jpn jpn Japan Japanese", "jpn jpn Japan Japanese",
				"kor kor South Korea Korean", "lav lav Latvia Latvian",
				"lit lit Lithuania Lithuanian", "mkd mkd Macedonia Macedonian",
				"mlt mlt Malta Maltese", "msa msa Malaysia Malay",
				"nld nld Netherlands Dutch", "nld nld Belgium Dutch",
				"nor nor Norway Norwegian", "nor nor Norway Norwegian",
				"pol pol Poland Polish", "por por Portugal Portuguese",
				"por por Brazil Portuguese", "ron ron Romania Romanian",
				"rus rus Russia Russian", "slk slk Slovakia Slovak",
				"slv slv Slovenia Slovenian", "spa spa Peru Spanish",
				"spa spa Guatemala Spanish", "spa spa Nicaragua Spanish",
				"spa spa Spain Spanish", "spa spa United States Spanish",
				"spa spa Mexico Spanish", "spa spa Uruguay Spanish",
				"spa spa Dominican Republic Spanish",
				"spa spa Venezuela Spanish", "spa spa Ecuador Spanish",
				"spa spa Colombia Spanish", "spa spa Costa Rica Spanish",
				"spa spa Chile Spanish", "spa spa Bolivia Spanish",
				"spa spa El Salvador Spanish", "spa spa Paraguay Spanish",
				"spa spa Puerto Rico Spanish", "spa spa Argentina Spanish",
				"spa spa Honduras Spanish", "sqi sqi Albania Albanian",
				"srp srp Bosnia and Herzegovina Serbian",
				"srp srp Serbia and Montenegro Serbian",
				"srp srp Serbia Serbian", "srp srp Montenegro Serbian",
				"swe swe Sweden Swedish", "tha tha Thailand Thai",
				"tha tha Thailand Thai", "tur tur Turkey Turkish",
				"ukr ukr Ukraine Ukrainian", "vie vie Vietnam Vietnamese",
				"zho zho Taiwan Chinese", "zho zho Singapore Chinese",
				"zho zho China Chinese", "zho zho Hong Kong Chinese" };
	}

	private String[] getRightList02() {
		return new String[] { "ara ara Lebanon Arabic",
				"ara ara United Arab Emirates Arabic", "spa spa Panama Spanish" };
	}

	private String[] getLeftList02() {
		return new String[] { "ara ara Iraq Arabic", "ara ara Yemen Arabic",
				"ara ara Qatar Arabic", "ara ara Saudi Arabia Arabic",
				"ara ara Kuwait Arabic", "ara ara Sudan Arabic",
				"ara ara Syria Arabic", "ara ara Bahrain Arabic",
				"ara ara Tunisia Arabic", "ara ara Jordan Arabic",
				"ara ara Egypt Arabic", "ara ara Morocco Arabic",
				"ara ara Algeria Arabic", "ara ara Libya Arabic",
				"ara ara Oman Arabic", "bel bel Belarus Belarusian",
				"bul bul Bulgaria Bulgarian", "cat cat Spain Catalan",
				"ces ces Czech Republic Czech", "dan dan Denmark Danish",
				"deu deu Switzerland German", "deu deu Austria German",
				"deu deu Germany German", "deu deu Luxembourg German",
				"ell ell Cyprus Greek", "ell ell Greece Greek",
				"eng eng United States English", "eng eng Malta English",
				"eng eng United Kingdom English",
				"eng eng New Zealand English", "eng eng Philippines English",
				"eng eng South Africa English", "eng eng Ireland English",
				"eng eng India English", "eng eng Australia English",
				"eng eng Canada English", "eng eng Singapore English",
				"est est Estonia Estonian", "fin fin Finland Finnish",
				"fra fra Belgium French", "fra fra Canada French",
				"fra fra Switzerland French", "fra fra Luxembourg French",
				"fra fra France French", "gle gle Ireland Irish",
				"heb heb Israel Hebrew", "hin hin India Hindi",
				"hrv hrv Croatia Croatian", "hun hun Hungary Hungarian",
				"ind ind Indonesia Indonesian", "isl isl Iceland Icelandic",
				"ita ita Italy Italian", "ita ita Switzerland Italian",
				"jpn jpn Japan Japanese", "jpn jpn Japan Japanese",
				"kor kor South Korea Korean", "lav lav Latvia Latvian",
				"lit lit Lithuania Lithuanian", "mkd mkd Macedonia Macedonian",
				"mlt mlt Malta Maltese", "msa msa Malaysia Malay",
				"nld nld Netherlands Dutch", "nld nld Belgium Dutch",
				"nor nor Norway Norwegian", "nor nor Norway Norwegian",
				"pol pol Poland Polish", "por por Portugal Portuguese",
				"por por Brazil Portuguese", "ron ron Romania Romanian",
				"rus rus Russia Russian", "slk slk Slovakia Slovak",
				"slv slv Slovenia Slovenian", "spa spa Peru Spanish",
				"spa spa Guatemala Spanish", "spa spa Nicaragua Spanish",
				"spa spa Spain Spanish", "spa spa United States Spanish",
				"spa spa Mexico Spanish", "spa spa Uruguay Spanish",
				"spa spa Dominican Republic Spanish",
				"spa spa Venezuela Spanish", "spa spa Ecuador Spanish",
				"spa spa Colombia Spanish", "spa spa Costa Rica Spanish",
				"spa spa Chile Spanish", "spa spa Bolivia Spanish",
				"spa spa El Salvador Spanish", "spa spa Paraguay Spanish",
				"spa spa Puerto Rico Spanish", "spa spa Argentina Spanish",
				"spa spa Honduras Spanish", "sqi sqi Albania Albanian",
				"srp srp Bosnia and Herzegovina Serbian",
				"srp srp Serbia and Montenegro Serbian",
				"srp srp Serbia Serbian", "srp srp Montenegro Serbian",
				"swe swe Sweden Swedish", "tha tha Thailand Thai",
				"tha tha Thailand Thai", "tur tur Turkey Turkish",
				"ukr ukr Ukraine Ukrainian", "vie vie Vietnam Vietnamese",
				"zho zho Taiwan Chinese", "zho zho Singapore Chinese",
				"zho zho China Chinese", "zho zho Hong Kong Chinese" };
	}

	private synchronized void pauseShort() {
		try {
			wait(200);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private synchronized void pauseMedium() {
		try {
			wait(1000);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

	private synchronized void pauseLong() {
		try {
			wait(4000);
		} catch (InterruptedException ex) {
			// Dont worry about that exception
		}
	}

}
